import {
  Controller,
  Post,
  UsePipes,
  ValidationPipe,
  Req,
  Body,
} from '@nestjs/common';
import { ApiBearerAuth } from '@nestjs/swagger';
import { plainToClass } from 'class-transformer';
import { Auth } from 'src/common/decorators/auth.decorator';
import { RequirePermission } from 'src/common/decorators/permission.decorator';
import { UpdateMatchDto } from './dto/update-match.dto';
import { MatchReadyService } from './match-ready.service';

@Auth()
@ApiBearerAuth()
@Controller('match')
export class MatchReadyController {
  constructor(private readonly matchReadyService: MatchReadyService) {}

  @RequirePermission(['1004002', '1004003'], 'OR')
  @Post('readyMatch')
  @UsePipes(new ValidationPipe({ transform: true }))
  async readyMatch(@Req() req, @Body() matchDataList: UpdateMatchDto[]) {
    const user = req.user;
    const dtoList = matchDataList.map((matchData) => {
      if (matchData.id) {
        matchData.updateBy = user.id;
      } else {
        matchData.createBy = user.id;
        matchData.updateBy = user.id;
      }
      return plainToClass(UpdateMatchDto, matchData);
    });
    // return this.matchService.saveList(dtoList);
    await this.matchReadyService.matchReady(dtoList);
  }
}
